<script type="text/javascript">
/*<![CDATA[*/
var app = angular.module('app', ['tm.pagination','ui.bootstrap','ui.router']);
app.config(function($stateProvider,$urlRouterProvider) {
    $urlRouterProvider.when('', '/page1');   //$urlRouterProvider初始化跳转的状态，首先想展示哪一个页面
    $stateProvider.state("home", {
            url: "/home?perPage=10&pageNum=0",
            controller: "MainController",
            templateUrl: "index.html"
        }
    );
});
app.controller('MainContentController', function ($rootScope, $scope, $modal, $http, $stateProvider) {
    $scope.images = ['pic.jpg', 'pic1.jpg', 'pic2.jpg', 'pic3.jpg'];
    $scope.data = {};
    $scope.rows = [];
    $scope.pageNum = 1;
alert(2)
    $scope.count = 0;
    $scope.p_pernum = 10;
    //变量
    $scope.p_current = 1;
    $scope.p_all_page = 0;
    $scope.pages = [];
    _get($scope.p_current, $scope.p_pernum, function () {
        alert("我是第一次加载");
    });
    //获取数据
    //初始化载入数据
    function _get(page, size, callback) {
        var pageNum = page - 1;
        $http({
            url: '/findAll?perPage=10&pageNum=' + pageNum,
            method: 'GET'
        }).success(function (rows) {
            if (rows.length > 0) {
                $scope.count = rows[0].num;
                $scope.p_all_page = Math.ceil($scope.count / $scope.p_pernum);
            } else {
                $scope.count = 0
                $scope.p_all_page = 0;
            }

            $scope.p_current = page;

            reloadPno();
            $scope.rows = [];
            for (var i in rows) {
                var row = rows[i];

                row.labelsList = rows[i].news;

                row.images = 'images/' + row.id + ".jpg"
                // row.labelsList = row.labelsList.substr(0,row.labelsList.indexOf('_')-1)
                $scope.rows.push(row);
            }

        });
    }

    function go() {
        alert(2)
        $state.go("home", {
            userId: id
        });

    }


    //首页
    $scope.p_index = function () {
        $scope.load_page(1);
    }
    //尾页
    $scope.p_last = function () {
        $scope.load_page($scope.p_all_page);
    }
    //加载某一页
    $scope.load_page = function (page) {
        _get(page, $scope.p_pernum, function () {
        });
    };

    //初始化页码
    function reloadPno() {
        $scope.pages = calculateIndexes($scope.p_current, $scope.p_all_page, 8);
    };

//分页算法
    function calculateIndexes(current, length, displayLength) {
        var indexes = [];
        var start = Math.round(current - displayLength / 2);
        var end = Math.round(current + displayLength / 2);
        if (start <= 1) {
            start = 1;
            end = start + displayLength - 1;
            if (end >= length - 1) {
                end = length - 1;
            }
        }
        if (end >= length - 1) {
            end = length;
            start = end - displayLength + 1;
            if (start <= 1) {
                start = 1;
            }
        }
        for (var i = start; i <= end; i++) {
            indexes.push(i);
        }
        return indexes;
    };


    var calculateIndexes = function (current, length, displayLength) {
        var indexes = [];
        var start = Math.round(current - displayLength / 2);
        var end = Math.round(current + displayLength / 2);
        if (start <= 1) {
            start = 1;
            end = start + displayLength - 1;
            if (end >= length - 1) {
                end = length - 1;
            }
        }
        if (end >= length - 1) {
            end = length;
            start = end - displayLength + 1;
            if (start <= 1) {
                start = 1;
            }
        }
        for (var i = start; i <= end; i++) {
            indexes.push(i);
        }
        return indexes;
    };

    //获取数据
    $scope.test = function (id) {
        $scope.getNum = $scope.rows[id];

        var modalInstance = $modal.open({
            templateUrl: '/newHtml.html',//script标签中定义的id
            controller: 'alertController',//modal对应的Controller
            title: '弹框标题',
            resolve: {
                data: function () {//data作为modal的controller传入的参数
                    return $scope.getNum;//用于传递数据
                }
            }
        })
    }
    //获取数据
    $scope.get = function (id) {
        $http({
            url: '/get?id=' + id,
            method: 'POST',
        }).success(function (data) {
            for (var i in $scope.rows) {
                var row = $scope.rows[i];
                if (data.id == row.id) {
                    row.no = data.no;
                    row.date = data.date;
                    row.quantity = data.quantity;
                    return;
                }
            }
            $scope.rows.push(data);
        });
    }

    function getMyDate(str) {
        str = parseInt(str);
        if (str != "" || str != null) {
            var oDate = new Date(str);
            var oYear = oDate.getFullYear();
            var oMonth = oDate.getMonth() + 1;
            oMonth = oMonth >= 10 ? oMonth : '0' + oMonth;
            var oDay = oDate.getDate();
            oDay = oDay >= 10 ? oDay : '0' + oDay;
            var theDate = oYear + "-" + oMonth + "-" + oDay;
        } else {
            theDate = "";
        }
        return theDate
    };

});
app.controller('alertController', function ($scope, $modalInstance, data) {
    $scope.getNum = data;
});



/*]]>*/
</script>